<template>
	<div class="index" id="index">
		<!--吸顶搜索-->
		<search-small ref="searchSmall" :shareTemplate="shareTemplate" v-if="scrollTop >= 140"></search-small>

		<!--搜索栏-->
		<search ref="search" :shareTemplate="shareTemplate"></search>

		<!-- 活动列表 -->
		<newSpecial></newSpecial>

		<!-- 限时抢购 -->
		<!-- <FlashSale></FlashSale> -->

		<!-- 品牌优选 -->
		<GoodBrand></GoodBrand>

		<!--商品列表-->
		<goods ref="goodsList"></goods>

		<!--淘宝授权-->
		<authorize
			ref="authorize"
			@close="closeAuthorize"
			v-if="(userInfo.tbStatus === 0 || userInfo.tbStatus === 2 || userInfo.tbStatus === 3) && authorizeStatus"
		></authorize>

		<!--Ai搜索推荐-->
		<!--<ai-search ref="aiSearch"></ai-search>-->

		<!--新人指引-->
		<user-guid ref="guid" @closeGuid="closeGuid" v-if="userInfo && userInfo.id"></user-guid>
	</div>
</template>

<script>
import store from 'store';
import { SearchSmall, Search, Goods, Authorize, UserGuid, newSpecial, FlashSale, GoodBrand } from './components';
import { mapGetters } from 'vuex';
import $ from 'jquery';

export default {
	name: 'index',
	components: {
		SearchSmall,
		Search,
		Goods,
		Authorize,
		// AiSearch,
		UserGuid,
		newSpecial,
		FlashSale,
		GoodBrand
	},
	data() {
		return {
			isRefresh: false,
			shareTemplate: [],
			authorizeStatus: true,
			newPerson: true,
			scrollTop: 0
		};
	},
	computed: {
		...mapGetters(['userInfo', 'isShowSearch'])
	},
	created() {
		this.getShareTemplate();
	},
	mounted() {
		window.addEventListener('scroll', () => {
			this.scrollTop = $(document).scrollTop();
		});
	},
	activated() {
		if (store.get('token') && store.get('token') !== 'undefined') {
			this.$store.dispatch('GET_USERINFO');
		}
	},
	methods: {
		getLocation() {
			BestpayHtml5.LBS.getLocation(
				{
					isUpdated: true,
					promptContent: '开启定位,以为您提供更好的服务',
					accuracy: 'high'
				},
				function(res) {
					console.log('success', res);
				},
				function(err) {
					console.log('error', err);
				},
				function() {
					console.log('channel');
				}
			);
		},
		// 本次关闭授权提示
		closeAuthorize() {
			this.authorizeStatus = false;
		},
		// 关闭新人指引
		closeGuid() {
			this.newPerson = false;
		},
		// 获取url参数
		getUrlKey(name) {
			return (
				decodeURIComponent(
					(new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')
				) || null
			);
		},
		// 获取分享文案
		getShareTemplate() {
			let params = {
				type: 100
			};
			this.$service.shopping.shareTemplate
				.list(params)
				.then(res => {
					this.shareTemplate = res;
				})
				.catch(err => {
					store.remove('token');
					window.location.reload();
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.index {
	padding-bottom: 100px;
}
</style>
